<template>
  <!-- 搜索区域 -->
  <ContentWrap>
    <ElForm
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="字典名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入字典名称"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字典类型" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入字典类型"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option
            v-for="dict in common_enable_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          type="daterange"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
        <el-button
          v-hasPermi="['system:dict:create']"
          plain
          type="primary"
          @click="openForm('create')"
        >
          <Icon class="mr-5px" icon="ep:plus" />
          新增
        </el-button>
      </el-form-item>
    </ElForm>
  </ContentWrap>

  <!-- 列表区域 -->
  <ContentWrap>
    <el-table v-loading="listLoading" :data="list">
      <el-table-column label="字典编号" prop="id" />
      <el-table-column label="字典名称" prop="name" show-overflow-tooltip />
      <el-table-column label="字典类型" prop="type" width="300" />
      <el-table-column label="状态" prop="status">
        <template #default="scope">
          <dict-tag :options="common_enable_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" show-overflow-tooltip />
      <el-table-column label="创建时间" prop="createTime" width="180" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:dict:update']"
          >
            编辑
          </el-button>
          <router-link :to="'/system/dict-data/' + scope.row.type">
            <el-button link type="primary">数据</el-button>
          </router-link>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:dict:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DictTypeForm ref="editFormRef" @success="getList" />
</template>

<script setup lang="ts">
import * as DictTypeApi from '@/api/system/dict/dict_type'
import DictTypeForm from './DictTypeForm.vue'

defineOptions({ name: 'SystemDictType' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { common_enable_status } = useIntDict(DICT_TYPE.common_enable_status)

const queryParams = reactive({
  name: undefined,
  type: undefined,
  status: undefined,
  createTime: [],
  pageNo: 1,
  pageSize: 50
})
const queryFormRef = ref()

const listLoading = ref(false)

const list = ref([]) // 列表数据
const total = ref(0) // 列表数据总条数

const getList = async () => {
  listLoading.value = true
  try {
    const data = await DictTypeApi.getDictTypePage(queryParams)
    list.value = data.content
    total.value = data.totalElement
  } finally {
    listLoading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleDelete = async (id: number) => {
  try {
    // 弹出确认框
    await message.delConfirm()
    // 发起删除请求
    await DictTypeApi.deleteDictType(id)
    // 成功提示
    message.success(t('common.delSuccess'))
    // 刷新列表(页码不变)
    await getList()
  } catch {}
}

const editFormRef = ref<InstanceType<typeof DictTypeForm>>()
const openForm = (type: string, id?: number) => {
  editFormRef.value?.open(type, id)
}

onMounted(() => {
  getList()
})
</script>

<style scoped lang="scss"></style>
